﻿using System;
using System.Text;

public class ThreeTasks
{
    public static int[] array;

    public static void Main()
    {
        switch (Choose())
        {
            case 1: { ReverseNumber(); break; }
            case 2: { CalculateAverage(); break; }
            case 3: { SolveLinearEquation(); break; }
            default: break;
        }
    }

    public static int Choose()
    {
        char choise;

        do
        {
            Console.Clear();
            Console.WriteLine("{0}{1}{2}{3}",
                "\nChoose Task!\n\n", 
                "Press 'R' to 'Reverses the digits of a number'\n", 
                "Press 'C' to 'Calculates the average of a sequence of integers\n",
                "Press 'S' to 'Solves a linear equation a*x + b = 0'\n");
            if (char.TryParse(Console.ReadLine(), out choise))
                switch (choise)
                {
                    case 'r':
                    case 'R': return 1;
                    case 'c':
                    case 'C': return 2;
                    case 's':
                    case 'S': return 3;
                    default: continue;
                }
        } while (true);
    }

    public static void ReverseNumber()
    {
        Common.Print(string.Format("Reverse number = {0}\n", 
            ReverseDigits.Reverse(Common.InputNonNegativeDecimal("\nNumber = ").ToString())));
    }

    public static void CalculateAverage()
    {
        decimal average = 0;

        array = Common.InputArray(array, "");
        for (int i = 0; i < array.Length; i++)
            average += array[i];

        average /= array.Length;
        Console.WriteLine("\nThe average of all members is {0}\n", average);
    }

    public static void SolveLinearEquation()
    {
        decimal a;
        decimal b;

        do
        {
            a = Common.InputDecimal("\nEnter coefficients of linear equation a*x + b = 0\n\n(0 != ) a = ");
            if (a != 0)
            {
                b = Common.InputDecimal(string.Format("\nEnter coefficients of linear equation a*x + b = 0\n\na = {0}\nb = ", a));
                break;
            }
        } while (true);

        Console.Clear();
        Console.WriteLine("\nThe unique real root of linear equation {0}*x{1}{2}=0 is\nx = {3}\n", 
            a, 
            0 < b ? "+" : (b < 0 ? "-" : ""), 
            0 < b ? b.ToString() : (b < 0 ? (-b).ToString() : ""), 
            (-b / a));
    }
}